package com.app.teaware.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.widget.TextView;

import com.app.teaware.basis.BasisApp;
import com.app.teaware.bean.LoginServiceListData;
import com.app.teaware.bean.UploadImgData;
import com.app.teaware.bean.UserUploadFileBean;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import my.CheckUtils;
import my.MySharedPreferences;
import my.StringUtil;

/**
 * Created by niany on
 */

public class MyUtils {

    private static final String SP_IP= "app_ip";
    private static final String SP_PORT= "app_port";
    public static final String SP_FILTER_WORDS = "ffapp_fir_filter_words";
    public static final String SP_HOME_INDEX_SELECT_NAME = "home_index_select_name";
    public static final String SP_HOME_INDEX_SELECT_ID = "home_index_select_id";



    public static String getHomeSelectId(){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        return msp.getString(SP_HOME_INDEX_SELECT_ID,"");
    }

    public static void putHomeSelectId( String id){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        msp.putString(SP_HOME_INDEX_SELECT_ID,id);
    }

    public static String getHomeSelectName(){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        return msp.getString(SP_HOME_INDEX_SELECT_NAME,"");
    }

    public static void putHomeSelectName( String name){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        msp.putString(SP_HOME_INDEX_SELECT_NAME,name);
    }


    public static String getIp(){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        if(TextUtils.isEmpty(msp.getString(SP_IP))){
            return "";
//            return  Constants.URL_SERVICE;
        }

//        String ip = msp.getString(SP_IP)+":"+msp.getString(SP_PORT);
        String ip = msp.getString(SP_IP);
        if(!ip.startsWith("http")){
            ip = "http://"+ip;
        }
        return ip;
    }

    public static void putIP( String ip){
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        msp.putString(SP_IP,ip);
    }


    public static String getServiceAPI(){
        return getIp();
    }





    public static  void putSerciceData(LoginServiceListData data){
        if(data == null)return;
        MySharedPreferences msp = new MySharedPreferences(BasisApp.getInstance());
        msp.putString(SP_IP,data.getIp());
    }


    public static ArrayList<UploadImgData> getListImgFromListFile(ArrayList<UserUploadFileBean> listorg){
        ArrayList<UploadImgData> list = new ArrayList<>();
        if(listorg == null || listorg.size() == 0)return  list;
        for(UserUploadFileBean fileBean:listorg){
            list.add(fileBean.getImgData());
        }
        return list;
    }

    public static ArrayList<UserUploadFileBean> getListFileFromImglist(ArrayList<UploadImgData> listorg){
        ArrayList<UserUploadFileBean> list = new ArrayList<>();
        if(listorg == null || listorg.size() == 0)return  list;
        for(UploadImgData fileBean:listorg){
            list.add(fileBean.getUploadFileData());
        }
        return list;

    }

    public static ArrayList<String> getImageFromImgData(ArrayList<UploadImgData> listImgs){
        if(listImgs == null || listImgs.size() == 0)return null;
        ArrayList<String> listStrs = new ArrayList<>();
        for(UploadImgData imgData:listImgs){
            listStrs.add(imgData.getPath());
        }
        return  listStrs;
    }


    /**
     * @param checkType 校验类型：0校验手机号码，1校验座机号码，2两者都校验满足其一就可
     * @param phoneNum
     * */
    public static boolean validPhoneNum(String checkType,String phoneNum){
        boolean flag=false;
        Pattern p1 = null;
        Pattern p2 = null;
        Matcher m = null;
        p1 = Pattern.compile("^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\\d{8})?$");
//        p1 = Pattern.compile("^(1[3-9]{1}\\\\d{9}|0\\\\d{2,3}-\\\\d{8})$");
//        p2 = Pattern.compile("^(0[0-9]{2,3}\\-)?([1-9][0-9]{6,7})$");
        p2 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");
        if("0".equals(checkType)){
            System.out.println(phoneNum.length());
            if(phoneNum.length()!=11){
                return false;
            }else{
                m = p1.matcher(phoneNum);
                flag = m.matches();
            }
        }else if("1".equals(checkType)){
            if(phoneNum.length()<11||phoneNum.length()>=16){
                return false;
            }else{
                m = p2.matcher(phoneNum);
                flag = m.matches();
            }
        }else if("2".equals(checkType)){
            if(!((phoneNum.length() == 11 && p1.matcher(phoneNum).matches())||(phoneNum.length()<16&&p2.matcher(phoneNum).matches()))){
                return false;
            }else{
                flag = true;
            }
        }
        return flag;
    }


    public static String[]  getAppList(Context context) {
        PackageManager pm =context.getPackageManager();
        // Return a List of all packages that are installed on the device.
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        ArrayList<String> listNames = new ArrayList<>();
        for (PackageInfo packageInfo : packages) {
            // 判断系统/非系统应用
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) // 非系统应用
            {
                System.out.println("MainActivity.getAppList, packageInfo=" + packageInfo.packageName);
//                LogUtil.i("读取到应用--》"+packageInfo.packageName+"----"+packageInfo.applicationInfo.loadLabel(pm));
                listNames.add(packageInfo.applicationInfo.loadLabel(pm)+"");
            } else {
                // 系统应用
            }
        }
        if(listNames.size() > 0){
            String[] names = new String[listNames.size()];
            for(int i = 0, l = listNames.size() ; i < l ; i++){
                names[i] = listNames.get(i);
            }
            return names;
        }
        return new String[0];
    }


    public static String getPhoneLastFour(Context context){
        MySharedPreferences msp = new MySharedPreferences(context);
        String phone = msp.getUser();
        if(TextUtils.isEmpty(phone))return "";
        if(!CheckUtils.isPhone(phone))return phone;
        if(phone.length() <= 4)return phone;
        return phone.substring(phone.length()-4);
    }

    public static ArrayList<String> CommonMsgGetList(Context context){
        MySharedPreferences msp = new MySharedPreferences(context);
        String commonStrs = msp.getString("commonmsg");
        if(TextUtils.isEmpty(commonStrs)){
            ArrayList<String> list = new ArrayList<>();
            list.add("请各单位汇报情况！");
            list.add("收到！");
            list.add("明白！");
            return  list;
        }
        return StringUtil.toListFromString2(commonStrs);
    }




    public static  String BytesToStr(byte[] bytes){
        if(bytes == null || bytes.length == 0)return "";
        StringBuilder builder = new StringBuilder();
        for (Byte a : bytes) {
            builder.append(String.format("%02X", a));
        }
        return HexUtil.hexStringToString(builder.toString());
    }
    public static  String ByteArrayToStr(List<Byte> bytes){
        if(bytes == null || bytes.size() == 0)return "";
        StringBuilder builder = new StringBuilder();
        for (Byte a : bytes) {
            builder.append(String.format("%02X", a));
        }
        return HexUtil.hexStringToString(builder.toString());
    }

    public static byte[] StrToHexToBytes(String str){
        if(TextUtils.isEmpty(str))return null;
        String hexStr = null;
        try {
            hexStr = HexUtil.chineseWordToGbk(str)+"0D0A";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return  HexUtil.hexStringToBytes(hexStr);
    }
    public static byte[] ActionStrToHexToBytes(String str){
        if(TextUtils.isEmpty(str))return null;
        String hexStr = null;
        try {
            hexStr = HexUtil.chineseWordToGbk(str);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        hexStr = hexStr+"0D0A";
        return  HexUtil.hexStringToBytes(hexStr);
    }

    public static void setNotNullText(TextView textView,String value){
        if(textView == null)return;
        if(TextUtils.isEmpty(value) || "null".equals(value)){
            textView.setText("");
        }else {
            textView.setText(value);
        }
    }
 
}
